package com.maxd.service.order;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.PublisherOrderDtoBean;
import com.maxd.respository.order.OrderJpaRepository;
import com.maxd.respository.order.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单service实现类
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderJpaRepository orderJpaRepository;
    @Autowired
    private OrderRepository orderRepository;


    @Override
    public Result delete(long id) {
        orderJpaRepository.deleteById(id);
        return ResultUtil.success();
    }

    @Override
    public Result save(PublisherOrderDtoBean publisherOrderDtoBean) {
        PublisherOrderDtoBean save = orderJpaRepository.save(publisherOrderDtoBean);
        return ResultUtil.success(save);
    }

    @Override
    public Result findByRelationId(Long relation_id, Pageable pageable) {
        return ResultUtil.success(orderRepository.findByRelationId(relation_id, pageable));
    }

    @Override
    public Result findByRelationIdAndStatus(Long relation_id, Integer tk_status, Pageable pageable) {
        return ResultUtil.success(orderRepository.findByRelationIdAndStatus(relation_id, tk_status, pageable));
    }

    @Override
    public Result getAdminList(String fromInfo, Integer tk_status, Pageable pageable, String relation) {
        if (tk_status == 0 && fromInfo.equals("all") && relation.equals("-1")) {
            return ResultUtil.success(orderJpaRepository.findAll(pageable)); //查询全部
        } else if (tk_status != 0 && fromInfo.equals("all") && relation.equals("-1")) {
            return ResultUtil.success(orderRepository.findByStatus(tk_status, pageable)); //根据订单状态查询
        } else if (tk_status == 0 && !fromInfo.equals("all") && relation.equals("-1")) {
            return ResultUtil.success(orderRepository.findByFrom(fromInfo, pageable));//根据渠道状态查询
        } else if (tk_status != 0 && !fromInfo.equals("all") && relation.equals("-1")) {
            return ResultUtil.success(orderRepository.findByFromAndStatus(tk_status, fromInfo, pageable));//根据渠道和订单状态查询
        } else if (tk_status != 0 && fromInfo.equals("all") && !relation.equals("-1")) {
            return ResultUtil.success(orderRepository.findByStatusAndRelation(tk_status, Long.valueOf(relation), pageable)); //根据订单和用户查询
        } else if (tk_status == 0 && !fromInfo.equals("all") && !relation.equals("-1")) {
            return ResultUtil.success(orderRepository.findByFromAndRelation(fromInfo, Long.valueOf(relation), pageable)); //根据渠道和用户查询
        } else if (tk_status != 0 && !fromInfo.equals("all") && !relation.equals("-1")) {
            return ResultUtil.success(orderRepository.findByFromAndRelationAndStatus(tk_status, fromInfo, Long.valueOf(relation), pageable)); //根据渠道和用户和订单状态查询
        } else if (tk_status == 0 && fromInfo.equals("all") && !relation.equals("-1")) {
            return ResultUtil.success(orderRepository.findByRelation(Long.valueOf(relation), pageable)); //根据用户状态查询
        } else {
            return ResultUtil.success(orderJpaRepository.findAll(pageable));
        }
    }

    @Override
    public Result pubSharePreFeeCount(Long relation_id, int tk_status) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date now = new Date();
        return ResultUtil.success(orderRepository.pubSharePreFeeCount(relation_id, tk_status, sdf.format(now)));
    }

    @Override
    public Result pubSharePreFeeLastMonthCount(Long relation_id, int tk_status) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar cal = Calendar.getInstance();
        //获取上个月的日期（上个月的今天）
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -1);
        Date date3 = cal.getTime();
        return ResultUtil.success(orderRepository.pubSharePreFeeCount(relation_id, tk_status, sdf.format(date3)));

    }


    @Override
    public Result getPaidCount(Long relation_id) {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
        Date now = new Date();
        return ResultUtil.success(orderRepository.getPaidCount(relation_id, sdf.format(now)));
    }

    @Override
    public Result getMonthPaidCount(Long relation_id) {
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM");
        Date now = new Date();
        return ResultUtil.success(orderRepository.getPaidCount(relation_id, sdf.format(now)));
    }

    @Override
    public Result findByRelationIdCount(String trade_id) {
        return ResultUtil.success(orderRepository.findByRelationIdCount(trade_id));
    }

    @Override
    public Result findByRelationIdList(Long relation_id) {
        return null;
    }


    @Override
    public Result findByTime(String time) {
        return ResultUtil.success(orderRepository.findByTime(time, 14));
    }

    @Override
    public Result selectOrderList(Pageable pageable,String phone,String nickName,String relationId,String gradeIndex) {
        return ResultUtil.success(orderRepository.selectOrderList(pageable,phone,nickName,relationId,gradeIndex));
    }

    @Override
    public List<Map<String,Object>> selectOrderLists(String phone, String nickName, String relationId){
        return orderRepository.selectOrderLists(phone, nickName, relationId);
    }

    @Override
    public Result selectOrderListByUserId(Long userId, Pageable pageable) {
        return ResultUtil.success(orderRepository.selectOrderListByUserId(userId,pageable));
    }

    @Override
    public Result countByTime(String time, Long relation_id) {
        return ResultUtil.success(orderRepository.countByTime(time, relation_id, 0));
    }
}
